package cn.yangliu.comm.tools;

import cn.yangliu.comm.CommConst;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 杨柳 on 2016/8/5 0005.
 * email:yangliu@buestc.com
 * tel:18523437817
 */
public class DateUtils {

    public final static String DATE = "yyyyMMddHHmmss";
    public static DateFormat FORMAT_TIME = getSimpleDateFormat(CommConst.TIME_PATTERN);
    public static DateFormat FORMAT_DATE = getSimpleDateFormat(CommConst.DATE_PATTERN);
    public static DateFormat FORMAT_DATE_TIME = getSimpleDateFormat(CommConst.DATETIME_PATTERN);

    private static SimpleDateFormat getSimpleDateFormat(String datePattern) {
        return new SimpleDateFormat(datePattern);
    }



    public static String getWeekDay() {
        Calendar calendar = Calendar.getInstance();
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        switch (w) {
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            case 7:
                return "星期日";
            default:
                return "";
        }
    }

    public static String date2String(Date date, String partten) {
        if (StringUtils.isEmpty(partten)) {
            throw new NullPointerException("partten参数不能为空");
        }
        if (date == null) {
            throw new NullPointerException("date参数不能为空");
        }
        return getFormatInstence(partten).format(date);
    }

    private static DateFormat getFormatInstence(String partten) {
        return getSimpleDateFormat(partten);
    }


    public static Date string2Date(String dateString, String partten) {
        if (StringUtils.isEmpty(partten)) {
            throw new NullPointerException("partten参数不能为空");
        }
        if (StringUtils.isEmpty(dateString)) {
            throw new NullPointerException("dateString参数不能为空");
        }
        Date date = null;
        try {
            date = getFormatInstence(partten).parse(dateString);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return date;
    }


    public static Date getBeforDaysDate(int days, Date date) {
        days = days - 2 * days;
        return getNumberOfDate(days, date);
    }

    public static Date getAfterDaysDate(int days, Date date) {
        return getNumberOfDate(days, date);
    }

    public static Date getDateOfType(int type, int num, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, num);
        return date;
    }

    public static Integer getCurrentYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    private static Date getNumberOfDate(int num, Date date) {
        if (date == null) {
            throw new NullPointerException("date参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, num);
        return calendar.getTime();
    }

    public static Date dateEndOfDay(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    public static Date addMinutes(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    public static Date addYears(Date date, int years) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        return calendar.getTime();
    }

    public static Date subtractDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - days);
        return calendar.getTime();
    }

}
